package cc.facesign.oss.controller;

import cc.facesign.common.utils.bean.R;
import cc.facesign.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@RestController
@RequestMapping("/oss/pic")
@Api(description = "阿里云oss图片操作")
public class OssController {

    @Autowired
    private OssService ossService;

    @ApiOperation("上传到oss，返回url")
    @PostMapping("/upload")
    public R upload(MultipartFile file) {
        String res = ossService.upload(file);
        return R.ok().data("url", res);
    }


    @ApiOperation("根据路径上传到阿里云，返回url")
    @GetMapping("/uploadByUrl")
    public R uploadByUrl(String url) {
        String res = ossService.uploadByUrl(url);
        return R.ok().data("url", res);
    }

    @ApiOperation("根据文件流上传到阿里云，返回url")
    @PostMapping("/uploadByFileStream")
    public R uploadByFileStream(InputStream in, String suffix) {
        String url = ossService.uploadByFileStream(in, suffix);
        return R.ok().data("url", url);
    }
}
